
package com.yimixian.app.user;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.R;
import com.yimixian.app.ui.CircleImageView;
import com.yimixian.app.util.CustomViewUtils;
import com.yimixian.app.util.UiUtils;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by liuletao on 2015/8/18.
 */
public class UserInfoEditViewItem extends LinearLayout {

    @InjectView(R.id.tv_left)
    public TextView tvLeft;
    @InjectView(R.id.img_head)
    public CircleImageView imgHead;
    @InjectView(R.id.tv_right)
    public TextView tvRight;
    @InjectView(R.id.right_arrow)
    public ImageView rightArrow;
    private boolean mDrawDividerBelow;

    public UserInfoEditViewItem(Context context) {
        super(context);
        initView();
    }

    public UserInfoEditViewItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public UserInfoEditViewItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.userinfo_edit_item, this);
        CustomViewUtils.setViewBackground(this,
                getResources().getDrawable(R.drawable.settings_item_bg));
        ButterKnife.inject(this);
    }

    /**
     * @param ID 左文字
     * @param isRightArrowShow 右边箭头是否显示
     * @param drawDividerBelow 是否有下划线
     */
    public void setStandardViews(int ID, boolean isRightArrowShow, boolean drawDividerBelow) {
        imgHead.setVisibility(View.GONE);
        tvLeft.setText(getContext().getResources().getString(ID));
        rightArrow.setVisibility(isRightArrowShow ? View.VISIBLE : View.INVISIBLE);
        mDrawDividerBelow = drawDividerBelow;
    }

    /**
     * @param right 右边文字
     */
    public void setTvRight(String right) {
        imgHead.setVisibility(View.GONE);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(StringUtils.isEmpty(right) ? "" : right);
    }

    /**
     * 返回图片
     * 
     * @return
     */
    public CircleImageView getImgHead() {
        imgHead.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.GONE);
        return imgHead;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int shortDividerMargin = getResources().getDimensionPixelSize(
                R.dimen.settings_item_left_margin);
        if (mDrawDividerBelow) {
            canvas.drawRect(
                    shortDividerMargin,
                    getHeight() - 1,
                    getWidth() - shortDividerMargin,
                    getHeight(),
                    UiUtils.getDividerPaint());
        }
    }
}
